package com.linkoog.devtools.view_remark.control;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowManagerListener;
import com.intellij.util.messages.MessageBusConnection;
import com.linkoog.devtools.view_remark.control.action.ShowAliasToggleAction;
import com.linkoog.devtools.view_remark.control.action.ShowPrefixToggleAction;
import com.linkoog.devtools.view_remark.control.action.ShowSubfixToggleAction;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ProjectToolWindowGearActionDecorator  implements ToolWindowManagerListener {

    private static final String PROJECT_TOOLWINDOW_ID = "Project";
    private final Project project;


    public ProjectToolWindowGearActionDecorator(Project project) {
        this.project = project;
        MessageBusConnection messageBusConnection = project.getMessageBus().connect(project);
        messageBusConnection.subscribe(ToolWindowManagerListener.TOPIC, this);
    }


    public void toolWindowsRegistered(@NotNull List<String> ids, @NotNull ToolWindowManager toolWindowManager) {
        final ToolWindow toolWindow = toolWindowManager.getToolWindow(PROJECT_TOOLWINDOW_ID);
        if (toolWindow == null) return;

        List<AnAction> actions = new ArrayList<>();
        actions.add(new ShowAliasToggleAction());
        actions.add(new ShowPrefixToggleAction());
        actions.add(new ShowSubfixToggleAction());

        final DefaultActionGroup actionGroup = new DefaultActionGroup(actions);
        toolWindow.setAdditionalGearActions(actionGroup);
    }

}
